﻿rem
	bbdoc: Helps drawing a lot
end rem
Type TCamera
	'Center position of the camera
	Field centerX:Int
	Field centerY:Int
	
	Function Create:TCamera()
		Local d:TCamera = New TCamera
		
		d.centerOn(player.x, player.y)
		Return d
	End Function
	
	'summary: Centers camera on x;y in game coordinates
	Method centerOn(x, y)
		Local relX, relY
		
		centerX = x
		centerY = y
		
		If centerX < gameW / 2 Then centerX = gameW / 2 - 1
		If centerX + (gameW / 2) > curmapW Then centerX = curmapW - gameW / 2
		If centerY < gameH / 2 Then centerY = gameH / 2 - 1
		If centerY + (gameH / 2) > curmapH Then centerY = curmapH - gameH / 2
	End Method
	
	'summary: Draws Chr(code) if it fits in viewport
	Method Draw(code, x, y)
		If Not between(x, centerX - (gameW / 2) - 2, centerX + (gameW / 2) + 1) Then Return 0
		If Not between(y, centerY - (gameH / 2) - 1, centerY + (gameH / 2)) Then Return 0
		DrawText(Chr(code), CntrX + ((x - centerX) * tileW), CntrY + ((y - centerY) * tileW))
		Return 1
	End Method
	
	'summary: Moves camera relatively to its position
	Method Move(x, y)
		'Не даем камере выйти за края карты
		If centerX - (gameW / 2) < 0 And x < 0 Then Return 0
		If centerX + (gameW / 2) > curmapW And x > 0 Then Return 0
		If centerY - (gameH / 2) < 0 And y < 0 Then Return 0
		If centerY + (gameH / 2) > curmapH And y > 0 Then Return 0
		'Даем шанс игроку встать в центр
		If player.x > centerX And x < 0 Then Return 0
		If player.x < centerX And x > 0 Then Return 0
		If player.y > centerY And y < 0 Then Return 0
		If player.y < centerY And y > 0 Then Return 0
		'And then move(possibly)
		centerX:+x
		centerY:+y
		Return 1
	End Method
End Type
